import Promise from 'promise'
import serData from './api-data'
import * as utils from '../utils/index'

const edTree = function (d) {
    d.forEach((v,i)=>{
        v.label = v.name;
        delete v.name;
        v.value = v.id;
        delete v.id;
        v.children = v.list;
        delete v.list;
        if(v.children&&v.children.length){
            edTree(v.children)
        }else {
            delete v.children
        }
    })
};
const edLabel = function (d) {
    d.forEach((v,i)=>{
        v.code = v.level;
        delete v.level;
    })
};
const edId = function (d) {
    d.forEach((v,i)=>{
        v.code = v.id;
        delete v.id;
    })
};

const STATUS = {
    //管理人员
    workerPerson:{
        status:[
            {code:'0',name:'禁用',btnClass:'bg-dark'},
            {code:'1',name:'启用',btnClass:'bg-success'},
        ],
    },
    //客商信息录入员
    workerWrite:{
        status:[
            {code:'-1',name:'审核中',btnClass:'bg-dark'},
            {code:'0',name:'禁用',btnClass:'bg-danger'},
            {code:'1',name:'启用',btnClass:'bg-success'},
        ],
    },
    //客户
    customer:{
        withStatus:[
            {code:'否',name:'否',btnClass:'bg-danger'},
            {code:'是',name:'是',btnClass:'bg-success'},
        ],
    },
    //站台
    station:[
        {code:"龙嘉",name:"龙嘉"},
        {code: "长春站",name: "长春站"},
        {code: "长春西站",name: "长春西站"}
    ],
};

let lazyData = {};
let lazyPromise = {};
const LAZY_INIT = (data,callback)=>{
    if(!lazyPromise[data.key]||data.update){
        lazyPromise[data.key] = $.Deferred();
        if(!lazyData[data.key]){lazyData[data.key] = [];}
        if(lazyData[data.key].length==0||data.update){
            require(`${data.serve}.js`).default[data.func]().then((res)=>{
                if(res.code == 1&&!!res.data) {
                    var arr = [];
                    utils.each(res.data,(i,v)=>{
                        if(callback){
                            arr.push(callback(v,i))
                        }else {
                            arr.push({code:v.id+'',name:v.name,btnClass:v.btnClass||''})
                        }
                    });
                    lazyData[data.key] =  arr;
                }else {
                    lazyData[data.key] = [{code:'-1',name:data.nilText||'无数据',btnClass:'bg-info'}];
                }
                lazyPromise[data.key].resolve(lazyData[data.key]);

            },function (error) {
                lazyPromise[data.key].reject(error);
            });
        }else {
            lazyPromise[data.key].resolve(lazyData[data.key]);
        }
    }
    return lazyPromise[data.key];
};
export const LAZY_STATUS = {
    mee_select:(update)=>{
        return LAZY_INIT({
            key:'mee_select',
            update:update,
            serve:'./api-conference',
            func:'listChildSelect',
            nilText:'无分会议数据',
        },(v,i)=>{
            return {code:v.code+'',name:v.name,btnClass:''}
        })
    },
};

export const GET_LAZY_STATUS = function (nameSplitText,or_obj,key,update) {
    var nameArr = nameSplitText.split('.'),obj=LAZY_STATUS;
    for(var i=0;i< nameArr.length;i++){
        if(obj[nameArr[i]]){obj = obj[nameArr[i]];
        }else{obj = ()=>$.Deferred();break;}
    }
    if(or_obj){
        let o_obj = {};
        return obj(update).then((res)=>{
            if(res.length>0&&res[0][key]!=undefined){
                for(var i=0;i< res.length;i++){
                    o_obj[res[i][key]] = res[i]
                }
                return o_obj
            }else {
                for(var i=0;i< obj.length;i++){
                    o_obj[i] = res[i]
                }
                return o_obj
            }
        });
    }else {
        return obj(update)
    }
};

export const GET_STATUS= function(nameSplitText,or_obj,key){
    var nameArr = nameSplitText.split('.'),obj=STATUS;
    for(var i=0;i< nameArr.length;i++){
        if(obj[nameArr[i]]){obj = obj[nameArr[i]];
        }else{obj = [];break;}
    }
    if(or_obj){
        let o_obj = {};
        if(obj.length>0&&obj[0][key]!=undefined){
            for(var i=0;i< obj.length;i++){
                o_obj[obj[i][key]] = obj[i]
            }
            return o_obj
        }else {
            for(var i=0;i< obj.length;i++){
                o_obj[i] = obj[i]
            }
            return o_obj
        }
    }else {
        return obj;
    }
};

/*远端配置项
* */
let FAR_STATUS = {};
export const SET_FAR_STATUS = function () {
    return new Promise(function (resolve, reject) {
        serData.all().then((res)=>{
            if(res.code==1){
                utils.each(res.data,(key,item)=>{
                    FAR_STATUS[key] = item;
                });
                resolve();
            }else{
                console.error('远端配置项获取错误');
                reject();
            }
        });
    });
};
export const GET_FAR_STATUS= function(nameSplitText,or_obj,key){
    if(!key){key = 'code'}
    var nameArr = nameSplitText.split('.'),obj=FAR_STATUS;
    for(var i=0;i< nameArr.length;i++){
        if(obj[nameArr[i]]){obj = obj[nameArr[i]];
        }else{obj = [];break;}
    }
    if(or_obj){
        let o_obj = {};
        if(obj.length>0&&obj[0][key]!=undefined){
            for(var i=0;i< obj.length;i++){
                o_obj[obj[i][key]] = obj[i]
            }
            return o_obj
        }else {
            for(var i=0;i< obj.length;i++){
                o_obj[i] = obj[i]
            }
            return o_obj
        }
    }else {
        return obj;
    }
};
